home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / vulpkgs / vulpkg4 / vulprog4.c < prev   
Encoding:
C/C++ Source or Header  |  1999-01-09  |  1.3 KB  |  60 lines

  1. /* 
  2.  * This is just a basic vulnerable program to demonstrate 
  3.  * how to overwrite/modify jmp_buf's to modify the course of 
  4.  * execution.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <setjmp.h>
  12.  
  13. #define ERROR -1
  14. #define BUFSIZE 16
  15.  
  16. static char buf[BUFSIZE];
  17. jmp_buf jmpbuf;
  18.  
  19. u_long getesp()
  20. {
  21.    __asm__("movl %esp,%eax"); /* the return value goes in %eax */
  22. }
  23.  
  24. int main(int argc, char **argv)
  25. {
  26.    if (argc <= 1)
  27.    {
  28.       fprintf(stderr, "Usage: %s <string1> <string2>\n");
  29.       exit(ERROR);
  30.    }
  31.  
  32.    printf("[vulprog] argv[2] = %p\n", argv[2]);
  33.    printf("[vulprog] sp = 0x%lx\n\n", getesp());
  34.  
  35.    if (setjmp(jmpbuf)) /* if > 0, we got here from longjmp() */
  36.    {
  37.       fprintf(stderr, "error: exploit didn't work\n");
  38.       exit(ERROR);
  39.    }
  40.  
  41.    printf("before:\n");
  42.    printf("bx = 0x%lx, si = 0x%lx, di = 0x%lx\n",
  43.           jmpbuf->__bx, jmpbuf->__si, jmpbuf->__di);
  44.  
  45.    printf("bp = %p, sp = %p, pc = %p\n\n", 
  46.           jmpbuf->__bp, jmpbuf->__sp, jmpbuf->__pc);
  47.  
  48.    strncpy(buf, argv[1], strlen(argv[1])); /* actual copy here */
  49.  
  50.    printf("after:\n");
  51.    printf("bx = 0x%lx, si = 0x%lx, di = 0x%lx\n",
  52.           jmpbuf->__bx, jmpbuf->__si, jmpbuf->__di);
  53.  
  54.    printf("bp = %p, sp = %p, pc = %p\n\n", 
  55.           jmpbuf->__bp, jmpbuf->__sp, jmpbuf->__pc);
  56.  
  57.    longjmp(jmpbuf, 1);
  58.    return 0;
  59. }
  60.